<template>
	<view :style="{height:height+'px'}">
		<view class="head" >
			<uni-search-bar
			ref="search"
			bgColorCls="bg-steelblue" 
			cancelColor="text-white"
			 :focus="focus" 
			 :placeholder="placeholder" 
			 @cancel = "cancel"
			 @confirm="confirm"
			></uni-search-bar>
			<view class="padding" style="font-size: 34rpx;">
				{{ tips+'('+((total || 0)+ '/'+ maxNum) + ')'}}
			</view>
			<view v-if="showInline">
				<scroll-view  class="padding-left" scroll-x style="background-color: #ffffff;width: 100%;white-space: nowrap; " :scroll-left="scrollLeft">
					<view class="" style="display: flex;">
						<view v-for="(item,index) of stackList" :key="index" class="inline-item" style="color: #606064;font-size: 34rpx;line-height: 90rpx;">
								<view class="inline-item" v-if="index==0" @click="backTree(item,-1)">
									<text v-if="index==stackList.length-1&&!isre" class="none">全部</text>
									<text v-else class="active">全部</text>
								</view>
							<view class="inline-item" @click="backTree(item,index)" v-if="index!=0" style="">
								<uni-icons v-if="index!=0" :type="'arrowright'" :color="'#666666'" size="18" style="padding:5px" />
								<text v-if="index==stackList.length-1" class="none" >
									{{ searchType == 'byPos' ? (item.posName || item.label):(item[label] || item.label)}}
								</text>
								<text v-else class="active">
									{{ searchType == 'byPos' ?(item.posName || item.label):(item[label] || item.label)}}
								</text>
							</view>
						</view>
						<view class="" v-if="stackList.length" style="padding: 0 20rpx 0 20rpx; color: #606064;font-size: 34rpx;line-height: 90rpx;;">
							|
						</view>
						<view class="" @click="returnShowType" v-if="stackList.length" style="color: #606064;font-size: 34rpx;line-height: 90rpx;;">
							返回
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="" v-if="showSearchType" :style="{'padding-top':stackList.length?'310rpx':'220rpx'}"  style="padding-bottom: 110rpx;">
			<uni-list>
				<uni-list-item  v-for="(item,index) of showMenuList" @click="searchTypeClick(item)" :key="item.id+''+index">
					{{item.label}}
				</uni-list-item>
			</uni-list>
			<checkbox-group v-if="defaultData.length"  class="padding-left" @change="checkboxChange" style="background-color: #ffffff">
				<view style="display: flex; flex-flow:row wrap;">
					<label v-for="item in items" :key="item[itemKey]" style="width:100%;border-bottom:1px solid #eee8e9 ;" >
						<view class="institution-dataList" >
							<view class="" >
								<checkbox
								 class='blue'
								 :class="item.checked?'checked':''"
								 :value="item.id+(item.isUser?'isUser':(item.isPos?'isPos':'isInst'))" 
								 :disabled="isDisable(item)"
								 :checked="item.checked"/>
							</view>
							<view v-if="item[namefield]" class="institution-name" style="display: flex; flex-direction: row;">
								<view class="" :style="{'max-width':(url?630:330)+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
									{{  searchType == "byPos"?item.instPosName:item[namefield]}}
								</view>
							</view>
						</view>
					</label>
				</view>
			</checkbox-group>
		</view>
		<view v-else style="padding-bottom: 90rpx;" :style="{'padding-top':showInline?(!showTree?'210rpx':'310rpx'):'200rpx'}">
				<checkbox-group v-if="!isUserPos && !isNew"  class="padding-left" @change="checkboxChange" style="background-color: #ffffff">
					<view style="display: flex; flex-flow:row wrap;">
						<label v-for="item in items" :key="item[itemKey]" style="width:100%;border-bottom:1px solid #eee8e9 ;" >
							<view class="institution-dataList" >
								<view class="">
									<checkbox
									 class='blue'
									 :class="item.checked?'checked':''"
									 :value="item.id+(item.isUser?'isUser':(item.isPos?'isPos':'isInst'))" 
									 :disabled="isDisable(item)"
									 :checked="item.checked"/>
								</view>
								<view v-if="item[label]" class="institution-name" style="display: flex; flex-direction: row;">
									<view v-if="!searchPos" class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
										{{  searchType == "byPos"?item.instPosName:item[label]}}
									</view>
									<view v-else class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
										{{  searchType == "byPos"?item.instPosName:(item[label2]?item[label2]:item[label])}}
									</view>
									<view class="" style="color:#a3a0a0;" v-if="showPeopleNum" >
										{{'('+(searchType == "byPos"?item.existsum:item.instSum) +'人)'}}
									</view>
								</view>
								<view class="institution-name" v-else>
									{{item[label2]}}
								</view>
								<view class="institution-button" v-if="!item.isUser">
									<view @click.stop="searchSelf(item)">
										{{'本级'}}
									</view>
									<view v-if="(searchType == 'byPos'?item.childPosSum:item.childrenSum)" class="institution-button-line" @click.stop="toChildren(item)">
										{{'下级'}}
									</view>
								</view>
							</view>
						</label>
					</view>
				</checkbox-group>
				<view v-else-if="!isUserPos && isNew" class="margin-left">
					<label v-for="(item,index) in items" :key="item[itemKey]"  style="width:100%;" >
						<view class="institution-dataList" :data-id="item.id" :data-value="index" :value="'' + item.id" :checked="item.checked" @click="checkboxChange1($event, item)">
							<view class="">
								<checkbox
								 class='blue'
								 :class="item.checked?'checked':''"
								 :value="item.id+''" 
								 :disabled="isDisable(item)"
								 :checked="item.checked"/>
							</view>
							<view v-if="item[label]" class="institution-name" style="display: flex; flex-direction: row;">
								<view v-if="!searchPos" class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
									{{  searchType == "byPos"?item.instPosName:item[label]}}
								</view>
								<view v-else class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
									{{  searchType == "byPos"?item.instPosName:(item[label2]?item[label2]:item[label])}}
								</view>
								<view class="" style="color:#a3a0a0;" v-if="showPeopleNum" >
									{{'('+(searchType == "byPos"?item.existsum:item.instSum) +'人)'}}
								</view>
							</view>
							<view class="institution-name" v-else>
								{{item[label2]}}
							</view>
							<view class="institution-button" v-if="!item.isUser">
								<view @click.stop="searchSelf(item)">
									{{'本级'}}
								</view>
								<view v-if="(searchType == 'byPos'?item.childPosSum:item.childrenSum)" class="institution-button-line" @click.stop="toChildren(item)">
									{{'下级'}}
								</view>
							</view>
						</view>
					</label>
				</view>
				<view v-else class="margin-left" style="display: flex; flex-flow:row wrap;">
					<label v-for="(item,index) in items" :key="item[itemKey]"  style="width:100%;" >
						<view v-if="item.posInfos">
							<view class="" v-for="pos in  item.posInfos">
								<view class="institution-dataList"  :data-id="pos.id" :data-value="index" :value="'' + pos.id" :checked="pos.checked" @click="checkboxChange1($event, pos,item)">
									<view class="" >
										<checkbox
										 class='blue'
										 :class="pos.checked?'checked':''"
										 :value="item.id + ''" 
										 :disabled="isDisable(item)"
										 :checked="pos.checked"/>
									</view>
									<view v-if="item[label]" class="institution-name" style="display: flex; flex-direction: row;">
										<view v-if="!searchPos" class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
											{{  searchType == "byPos"?item.instPosName:item[label]}}
										</view>
										<view v-else class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
											{{  searchType == "byPos"?item.instPosName:(item[label2]?item[label2]:item[label])}}
										</view>
										<view class="" style="color:#a3a0a0;" v-if="showPeopleNum" >
											{{'('+(searchType == "byPos"?item.existsum:item.instSum) +'人)'}}
										</view>
									</view>
									<view class="institution-name" v-else>
										{{item[label2] +( true?'/'+pos.posName:'') }}
									</view>
									<view class="institution-button" v-if="!item.isUser">
										<view @click.stop="searchSelf(item)">
											{{'本级'}}
										</view>
										<view v-if="(searchType == 'byPos'?item.childPosSum:item.childrenSum)" class="institution-button-line" @click.stop="toChildren(item)">
											{{'下级'}}
										</view>
									</view>
								</view>
							</view>
						</view>
						<view v-else>
							<view class="institution-dataList" :data-id="item.id" :data-value="index" :value="'' + item.id" :checked="item.checked" @click="checkboxChange1($event, item)">
								<view class="">
									<checkbox
									 class='blue'
									 :class="item.checked?'checked':''"
									 :value="item.id+''" 
									 :disabled="isDisable(item)"
									 :checked="item.checked"/>
								</view>
								<view v-if="item[label]" class="institution-name" style="display: flex; flex-direction: row;">
									<view v-if="!searchPos" class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
										{{  searchType == "byPos"?item.instPosName:item[label]}}
									</view>
									<view v-else class="" :style="{'max-width':630+'rpx'}" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
										{{  searchType == "byPos"?item.instPosName:(item[label2]?item[label2]:item[label])}}
									</view>
									<view class="" style="color:#a3a0a0;" v-if="showPeopleNum" >
										{{'('+(searchType == "byPos"?item.existsum:item.instSum) +'人)'}}
									</view>
								</view>
								<view class="institution-name" v-else>
									{{item[label2]}}
								</view>
								<view class="institution-button" v-if="!item.isUser">
									<view @click.stop="searchSelf(item)">
										{{'本级'}}
									</view>
									<view v-if="(searchType == 'byPos'?item.childPosSum:item.childrenSum)" class="institution-button-line" @click.stop="toChildren(item)">
										{{'下级'}}
									</view>
								</view>
							</view>
						</view>
					</label>
				</view>
		</view>
		<view class="foot">
			<view class="padding-left margin-top-xs foot-left" @click="footClick">
				<view class="flex">
					<view class="foot-left-top">{{'已选择:'+showCheckText.title}}</view>
					<uni-icons v-if="newCheckList.length" :type="'arrowup'" :color="'#5697F4'" size="18" />
				</view>
				<view class="foot-left-bottom">{{showCheckTitle}}</view>
			</view>
			<view class="foot-right" >
				<view class="foot-right-button om-btn-color" @click="footBtnClick">
					{{'确定'}}
				</view>
			</view>
		</view>
		<uni-popup id="popup" ref="popup" :type="'bottom'">
			<view :style="{height:height*1.6+'rpx'}" style="width: 100%; border-radius: 25rpx 25rpx 0rpx 0rpx; background-color: #FFFFFF;display: inline-block;">
				<view class="popup-title flex" style="width: 100%;border-bottom: 1px solid #dedede;height: 100rpx;margin-top: 25rpx;">
					<view class="popup-title-left padding-left" style="display: flex; justify-content: center;flex-direction:column;font-size: 32rpx;font-weight: 600;width: 82%;">
						{{'已选择:'+showCheckText.title}}
					</view>
					<view @click="popUpBtnClick" class="popup-title--button om-color" style="font-size: 32rpx;font-weight: 400;margin-left:5%;display: flex;justify-content: center;flex-direction:column;">
						确认
					</view>
				</view>
				<scroll-view scroll-y style="height: 87%;">
					<view class="" style="border-bottom: 5px solid #dedede;">
						<view class="flex" style="border-bottom: 1px solid #dedede;width:100%; height: 100rpx;justify-content: center;align-items: center;" v-for="(item,index) of instList" :key="index+'inst'">
							<view class="popup-title-left padding-left" style="font-size: 30rpx;width: 82%;">
								{{item.isPos?item.posName +'('+ item.existsum+ ')':item[label] +'('+ item.instSum+ ')'  }}
							</view>
							<view @click="removePopUpItem(item,index)" class="popup-title-button" style="border: 1px solid #cccccc;height: 55rpx;width: 90rpx; font-size: 28rpx;display: flex;justify-content: center;align-items: center;">
								移除
							</view>
						</view>
					</view>
					<view class="">
						<view class="flex" style="border-bottom: 1px solid #dedede;width:100%; height: 100rpx;justify-content: center;align-items: center;" v-for="(item,i) of userList" :key="i+'user'">
							<view class="popup-title-left padding-left" style=" ;font-size: 30rpx;width: 82%;">
								{{ isUserPos?(item['posName']?item[label2]+'/'+item['posName']:item[label2]):item[label2] }}
							</view>
							<view @click="removePopUpItem(item,i)" class="popup-title-button" style="border: 1px solid #cccccc;height: 55rpx;width: 90rpx; font-size: 28rpx;display: flex;justify-content: center;align-items: center;">
								移除
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js';
	
	export default{
		data(){
			return{
				menuList:[
					{
						label:'按组织架构选择',
						id:-1
					},
					{
						label:'按角色选择',
						id:-2,
						children:[
							{
								label:'层级属性',
								id:-21
							}
						]
					}
				],
				chooseType:'multitudinous',
				maxNum:100,
				total:0,
				title:'',
				searchType:'',
				preSearchType:'',
				showMenuList:[],
				showSearchType:true,
				stackList:[],
				scrollLeft:999,
				isre:false,
				label:'instName',
				label2:'username',
				newCheckList:[],
				newCheckListStr:[],
				items: [],
				height:0,
				focus:false,
				placeholder:'请输入姓名、机构、岗位',
				instList:[],
				userList:[],
				searchUser:false,
				showUserList:false,
				cachename:'',
				isFormUser:'',
				selectModel:'',
				tips:'人员选择(可选择所有人员)',
				url:'',  //路径
				other: {}, //参数
				acquiescence:false  ,//是否根据默认形式选择
				page:1,
				row:20,
				itemKey:'value',
				showTree:true,
				showPeopleNum:true,
				paging:false ,//是否分页
				searchfield:'',
				stopGetData:false,
				defaultData:[],
				namefield:'',
				searchPos:'',
				showPosInfos:false,
				label3:'',
				label4:'posId',
				isUserPos:false,
				isNew:true,
				showInline:true,
				userPosUrl:'',
				userPosParam:''
			}
		},
		watch:{
			searchUser:{
				handler(newVal,oldVal){
					this.$forceUpdate()
				}
			}
		},
		computed:{
			...mapState(useLoginUserStore, ['orgId','userId','salaryInstName', 'salaryPosName']),
			isDisable(){
				return function(item){
					if(!item.isUser){
						if(this.searchPos){
							return true
						}else{
							if((item.instSum + this.total > this.maxNum || item.existsum + this.total > this.maxNum) && !item.checked){
								return true
							}else{
								return false
							}
						}
					}else{
						if((1+this.total > this.maxNum) && !item.checked  ){
							return true
						}else{
							return false
						}
					}
				}
			},
			showCheckText(){
				let _that = this 
						let num = 0
						let hasChildren = 0
						let hasPos = 0
						for(let item of _that.newCheckList){
							if(item.isUser){
								num += 1
							}else{
								if(item.isPos){
									hasPos += 1
									num += item.existsum
								}else{
									hasChildren += 1
									num += item.instSum
								}
							}
						}
						this.total = num
						this.title = (num+'人')
						if(_that.newCheckList.length){
							if(hasChildren != 0){
								this.title += (',其中'+hasChildren+'个部门')  
							}
							if(hasPos != 0){
								this.title += (',其中'+hasPos+'个岗位')  
							}
							return {
								title:this.title
							}
						}else{
							return {
								title:''
							}
						}
			},
			showCheckTitle(){
				let str = ""
				if(this.newCheckList.length){
					str = ''
					for(let i = this.newCheckList.length - 1;i>=0;i--){
						let e = this.newCheckList[i]
						if(e.isUser){
							let text = ''
							if(this.isUserPos){
								if(e['posName']){
									text = e[this.label2]+'/'+e['posName']
								}else{
									text = e[this.label2]
								}
								
							}else{
								text = e[this.label2]
							}
							 
							let result = text.split('').reverse().join("")
							str += result+','
						}else if(e.isPos){
							let text = ''
							text = e.posName+')'+ e.existsum+'('
							let result = text.split('').reverse().join("")
							str += result+','
						}else{
							let text = ''
							text = e[this.label]+')'+ e.instSum+'('
							let result = text.split('').reverse().join("")
							str += result+','
						}
					}
					return str.substr(0,str.length-1)
				}else{
					let result = str.split('').reverse().join("")
					return result
				}
			}
		},
		methods:{
			getUserlist(url,param){
				let _that = this
				_that.om.request(
					url,{userIds:param},'post'
				).then(res=>{
					for(let userItem of res.value){
						_that.$set(userItem,'isUser',true)
						for(let pos of userItem.posInfos){
							for(let checkItem of _that.newCheckList){
								if(checkItem.isUser && checkItem[_that.label4] == pos[_that.label4]){
									_that.$set(pos,'isUser',true)
									_that.$set(pos,'checked',true)
								}
							}
						}
					}
					_that.items = res.value
				})
			},
			cancel(){
				let _that = this
				if(_that.url){
					_that.other.page = 1
					_that.items = []
					this.$set(this.other,this.searchfield,'')
					_that.getDataList2()
				}
			},
			getDataList2(type){
				let _that = this
				_that.om.request(
						_that.url,
						_that.other
					).then(res=>{
						if(!_that.paging){
							if(res.length || res.value.length){
								if(res.value.length){
									this.searchUser = true
									this.showSearchType = false
									this.preSearchType = this.searchType
									this.searchType = 'bySearchBar'
									if(!this.stackList.length){
										this.stackList.push(1)
									}
									for(let item of res.value){
										_that.$set(item,'checked',false)
										_that.$set(item,'isUser',true)
										for(let checkItem of _that.newCheckList){
											if(checkItem.id == item.id){
												if(checkItem.isUser){
													_that.$set(item,'checked',true)
												}
											}
										}
									}
									_that.items = res.value
								}else{
									for(let item of res){
										_that.$set(item,'checked',false)
										_that.$set(item,'isUser',true)
										for(let checkItem of _that.newCheckList){
											if(checkItem.id == item.id){
												if(checkItem.isUser){
													_that.$set(item,'checked',true)
												}
											}
										}
									}
									_that.items.push.apply(_that.items, res)
								}
							}else{
								_that.om.toast("没有匹配到任何结果")
							}
						}else{
							for(let item of res.rows){
								_that.$set(item,'checked',false)
								_that.$set(item,'isUser',true)
								for(let checkItem of _that.newCheckList){
									if(checkItem.id == item.id){
										if(checkItem.isUser){
											_that.$set(item,'checked',true)
										}
									}
								}
							}
							if(type == 'searchBar'){
								_that.items = res.rows
							}else{
								if(res.rows.length <_that.row){
									_that.stopGetData = true
								}else{
									_that.stopGetData = false
								}
								_that.items.push.apply(_that.items, res.rows);
							}
						}
						
					})
			},
			findPoslistByRequest(posIds){
				let _that = this
				_that.om.request(
					"posOfficeView/findPoslistByRequest",
					{
						posIds:(posIds?posIds:'')
					},
					'post'
				).then(res=>{
					for(let item of res.value){
						_that.$set(item,'checked',false)
						_that.$set(item,'isUser',true)
					}
					_that.items = res.value
				})
			},
			getDataList1(id){
				let _that = this
				return _that.om.request(
						"tposition/getTreeGrid",
						{
							orgId:_that.orgId,
							id:id?id:''
						}
					)
			},
			getCombobox(){
				let _that = this
				_that.om.request(
					'tsysdict/getCombobox',
					{
						code:'38627'
					}
				).then(res=>{
					let arr = []
					for(let item of res){
						let obj = {
							label:item.dicname,
							id:item.id,
							attribute:true
						}
						arr.push(obj)
					}
					this.showMenuList = arr
				})
			},
			returnShowType(){
				let _that = this
				_that.stackList = []
				_that.searchType = "byOri"
				if(!_that.showSearchType){
					_that.showSearchType = !_that.showSearchType
					_that.showMenuList = _that.menuList
				}else{
					_that.showMenuList = _that.menuList
				}
				_that.searchUser = false
				_that.items = _that.defaultData
			},
			searchTypeClick(item){
				let _that = this
				_that.items = []
				if(item.id == -1){
					_that.searchType = "byOri"
					_that.getDataList({id:''}).then(res=>{
						_that.showSearchType = !_that.showSearchType
						for(let resItem of res.value){
								resItem['checked'] = false
								resItem['isInst'] = true
							for(let checkItem of _that.newCheckList){
								if(checkItem.id == resItem.id){
									if(checkItem.isInst){
										_that.$set(resItem,'checked',true)
									}
								}
							}
						}
						_that.items = res.value
						_that.stackList.push(1)
					})
				}else if(item.id == -2){
					_that.searchType = "byRole"
					_that.showMenuList = _that.menuList[1].children
					_that.stackList.push(1)
					_that.$forceUpdate()
				}else if(item.id == -21){
					_that.searchType = "byLevel"
					_that.stackList.push(item)
					_that.getCombobox()
				}else if(item.id == -22){
					_that.searchType = "byPos"
					_that.getDataList1().then(res=>{
						if(_that.stackList.length>=1){
							_that.stackList.push(item)
						}else{
							_that.stackList.push(1)
						}
						for(let resItem of res){
							_that.$set(resItem,'checked',false)
							_that.$set(resItem,'isPos',true)
							for(let checkItem of _that.newCheckList){
								if(checkItem.id == resItem.id){
									if(checkItem.isPos){
										_that.$set(resItem,'checked',true)
									}
								}
							}
						}
						_that.showSearchType = !_that.showSearchType
						_that.items = res
					})
				}else{
					_that.showSearchType = !_that.showSearchType
					if(_that.stackList.length>1){
						_that.stackList.push(item)
					}else{
						_that.stackList.push(1)
					}
					let url = ''
					if(_that.searchPos){
						url ='posOfficeView/findPoslistByRequest' 
					}else{
						url = "orgInstPosUserOfficeView/findUserlistByRequest"
					}
					_that.om.request(
						url,
						{
							instId:item.attribute?'':item.id,
							attributeId:item.attribute?item.id:''
						},
						'post'
					).then(res=>{
						for(let userItem of res.value){
							userItem['checked'] = false
							userItem['isUser'] = true
							if(_that.isUserPos){
								for(let pos of userItem.posInfos){
									for(let checkItem of _that.newCheckList){
										if(checkItem.isUser && checkItem[_that.label4] == pos[_that.label4]){
											_that.$set(pos,'isUser',true)
											_that.$set(pos,'checked',true)
										}
									}
								}
							}else{
								for(let checkItem of _that.newCheckList){
									if(checkItem.isUser && checkItem.id == userItem.id){
										_that.$set(userItem,'checked',true)
									}
								}
							}
						}
						_that.items = res.value
					})
				}
			},
			footBtnClick(){
				let _that = this
				let arr = [] 
				let userList = []
				if(!_that.acquiescence){
					uni.setStorage({
						key:_that.cachename,
						data:_that.newCheckList
					})
					uni.navigateBack()
				}else{
					for(let item of _that.newCheckList){
						if(!item.isUser){
							arr.push(_that.om.request(
								"orgInstPosUserOfficeView/findUserlistByRequest",
								{
									instId:item.isInst?item.id:'',
									posId: item.isPos?item.id:''
								},
								'post',false
							))
						}else{
							userList.push(item)
						}
					}
					uni.showLoading({
						mask: true,
						title: '请稍候...'
					})
					Promise.all(arr).then(res=>{
						let obj ={};
						let arr = []
						for(let resArr of res){
							for(let user of resArr.value){
								arr.push(user)
							}
						}
						arr.push(...userList)
						if(_that.isUserPos){
							let poslist = []
							for(let item of arr){
								if(!item.posId){
									for(let pos of item.posInfos){
										_that.$set(pos,'id',item.id)
										_that.$set(pos,'username',item.username)
										poslist.push(pos)
									}
								}else{
									poslist.push(item)
								}
							}
							arr = poslist
							var m = new Map();
							arr = arr.filter((ele) => !m.has(ele.id+'/'+ele.posId) && m.set(ele.id+'/'+ele.posId, ""));
						}else{
							arr = arr.reduce((cur, next) => {
							    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
							    return cur;
							},[]);//设置cur默认类型为数组,并且初始值为空的数组
						}
						uni.hideLoading()
						if(_that.isFormUser){
							let pages = getCurrentPages();
							if (pages.length > 2) {
								let prePage = pages[pages.length - 2]
								//#ifdef APP-PLUS
								for (var iref in prePage.$vm.$refs) {
									var autoform = prePage.$vm.$refs[iref]
									let idStr = []
									let nameStr = []
									let formUserIdArr = ''
									if(autoform.formdata.formUserIds){
										formUserIdArr = autoform.formdata.formUserIds.split(',')
									}
									for(let index in arr){
										if(formUserIdArr.indexOf(arr[index].id+'') == -1){
											 idStr.push(arr[index].id)
											 nameStr.push(arr[index].username)
										}
									autoform.formdata.formUserIds = idStr.map(e =>e).join(",")
									autoform.formdata.formUserNames =nameStr.map(e =>e).join(",");
									}
									break
								}
								//#endif
								//#ifndef APP-PLUS
								for (var iref in prePage.$refs) {
									var autoform = prePage.$refs[iref]
									let idStr = []
									let nameStr = []
									let formUserIdArr = ''
									if(autoform.formdata.formUserIds){
										formUserIdArr = autoform.formdata.formUserIds.split(',')
									}
									for(let index in arr){
										idStr.push(arr[index].id)
										nameStr.push(arr[index].username)
									}
									autoform.formdata.formUserIds = idStr.map(e =>e).join(",")
									autoform.formdata.formUserNames =nameStr.map(e =>e).join(",");
									break
								}
								//#endif
								uni.navigateBack()
							} else {
								uni.switchTab({
									url:'/pages/main/main/main'
								})
							}
						}else{
							uni.setStorage({
								key:_that.cachename,
								data:arr
							})
							uni.navigateBack()
						}
						
					})
				}
			},
			searchCancel(){
				this.searchUser = true
				this.backTree(this.stackList[0],0)
			},
			confirm(e){
				if(!this.url){
					this.searchUser = true
					this.showSearchType = false
					this.preSearchType = this.searchType
					this.searchType = 'bySearchBar'
					if(!this.stackList.length){
						this.stackList.push(1)
					}
					this.searchSelf(e.value)
				}else{
					this.$set(this.other,this.searchfield,e.value)
					this.getDataList2('searchBar')
				}
			},
			popUpBtnClick(){
				this.$refs.popup.close()
			},
			removePopUpItem(item,i){
				this.$set(item,'checked',false)
				if(item.isUser){
					for(let index in this.newCheckList){
						if(this.isUserPos){
							if(this.newCheckList[index][this.label4] == item[this.label4] && item.isUser){
								this.newCheckList.splice(index,1)
								this.newCheckListStr.splice(index,1)
							}
						}else{
							if(this.newCheckList[index].id == item.id && item.isUser){
								this.newCheckList.splice(index,1)
								this.newCheckListStr.splice(index,1)
							}
						}
					}
					this.userList.splice(i,1)
				}else{
					for(let index in this.newCheckList){
						if(this.newCheckList[index].id == item.id && !item.isUser){
							this.newCheckList.splice(index,1)
							this.newCheckListStr.splice(index,1)
						}
					}
					this.instList.splice(i,1)
				}
				this.$forceUpdate()
			},
			footClick(){
				this.userList = []
				this.instList = []
				for(let item of this.newCheckList){
					if(item.isUser){
						this.userList.push(item)
					}else{
						this.instList.push(item)
					}
				}
				this.$refs.popup.open()
			},
			searchSelf(item){
				let _that = this
				if(_that.userPosUrl){
					return
				}
				_that.items = []
				let byInst = true
				if(Object.prototype.toString.call(item) != '[object Object]'){
					byInst = false
				}else{
					this.showUserList = true
				}
				let param = {}
				let method = 'post'
				if(_that.searchType == 'byPos'){
					param = {
						posId:item.id
					}
				}else{
					param = {
						instId:byInst?item.id:'',
						searchName:byInst?'':item
					}
				}
				let url = ''
				if(_that.searchPos){
					url ='posOfficeView/findPoslistByRequest'
				}else{
					url = "orgInstPosUserOfficeView/findUserlistByRequest"
				}
				_that.om.request(
					url,
					param,
					method
				).then(res=>{
					for(let userItem of res.value){
						_that.$set(userItem,'isUser',true)
						if(_that.isUserPos){
							for(let pos of userItem.posInfos){
								for(let checkItem of _that.newCheckList){
									if(checkItem.isUser && checkItem[_that.label4] == pos[_that.label4]){
										_that.$set(pos,'isUser',true)
										_that.$set(pos,'checked',true)
									}
								}
							}
						}else{
							if(userItem['checked'] == undefined){
								userItem['checked'] = false
							}
							if(!userItem.isUser){
								_that.$set(userItem,'isUser',true)
							}
							for(let checkItem of _that.newCheckList){
								if(checkItem.isUser && checkItem.id == userItem.id){
									_that.$set(userItem,'checked',true)
								}
							}
						}
					}
					_that.items = res.value
					if(byInst){
						_that.stackList.push(item);	
					}
					_that.$nextTick(() => {
							_that.scrollLeft += 200;
						})
					_that.$forceUpdate()
				})
			},
			getDataList(item){
				let _that = this
				return this.om.request(
					"tinstitution/findTopOrChildren",
					{
						id:item?item.id:''
					}
				)
			},
			checkboxChange1(e,item,pos){
				let _that = this
				let ncLength = _that.newCheckList.length
				let itLength = item.instSum
				if(!item.checked){
					if(item.instSum){
						if((itLength + ncLength)>_that.maxNum){
							_that.om.toast('不能超过最大人数'+_that.maxNum + '人')
							return
						}
					}else{
						if((ncLength + 1) > _that.maxNum){
							_that.om.toast('不能超过最大人数'+_that.maxNum + '人')
							return
						}
					}
				}
				_that.$set(item,'checked',!item.checked)
				if(item.checked){
					if(item.isInst){
						_that.$set(item,'isUser',false)
					}else{
						if(pos){
							_that.$set(item,_that.label2,pos[_that.label2])
							_that.$set(item,'isUser',true)
							_that.$set(item,_that.label3,pos[_that.label3])
						}else{
							if(!item.isPos){
								_that.$set(item,'isUser',true)
							}
						}
					}
					_that.newCheckList.push(item)
				}else{
					if(item.isInst){
						for(let checkIndex in _that.newCheckList){
							if(_that.newCheckList[checkIndex].code == item.code && _that.newCheckList[checkIndex].id == item.id){
								_that.newCheckList.splice(Number(checkIndex),1)
							}
						}
					}else{
						if(_that.isUserPos){
							for(let index in _that.newCheckList){
								if( !_that.newCheckList[index].code && _that.newCheckList[index][_that.label4] == item[_that.label4] ){
									_that.newCheckList.splice(Number(index),1)
								}
							}
						}else{
							for(let index in _that.newCheckList){
								if( !_that.newCheckList[index].code && _that.newCheckList[index].id == item.id+'' ){
									_that.newCheckList.splice(Number(index),1)
								}
							}
						}
					}
				}
			},
			//选中或取消
			checkboxChange(e){
				var items = this.items,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.indexOf(item.id+'isInst') >= 0 || values.indexOf(item.id+'isUser') >= 0 || values.indexOf(item.id+'isPos') >= 0){	
						if(item.isUser && this.newCheckListStr.indexOf(item.id+'isUser') == -1){
								this.newCheckListStr.push(item.id+'isUser')
						}else if(item.isPos && this.newCheckListStr.indexOf(item.id+'isPos') == -1){
								this.newCheckListStr.push(item.id+'isPos')
						}else if(item.isInst && this.newCheckListStr.indexOf(item.id+'isInst') == -1){
								this.newCheckListStr.push(item.id+'isInst')
						}else{
							continue
						}
						// 多选 
							let num = 0
							if(item.isPos){
								num = item.existsum + this.total
							}else if(item.isInst){
								num = item.instSum + this.total
							}else{
								num = 1 + this.total
							}
							if(this.chooseType =='multitudinous'){
								if(num > this.maxNum){
									this.newCheckListStr.splice(this.newCheckListStr.length-1,1)
									return
								}else{
									this.$set(item,'checked',true)
								}
							}else{
								if(num > this.maxNum){
									this.newCheckListStr = []
									return
								}else{
									this.$set(item,'checked',true)
								}
							}
							this.newCheckList.push(item)
						if(this.searchUser){
							this.searchUser = !this.searchUser
							this.showMenuList = this.menuList
							this.showSearchType = true
							this.stackList = []
						}
					}else{
						this.$set(item,'checked',false)
						if(item.isUser){
							for(let uIndex in this.newCheckList){
								if(this.newCheckList[uIndex].id == item.id && this.newCheckList[uIndex].isUser){
									this.newCheckList.splice(uIndex,1)
									this.newCheckListStr.splice(uIndex,1)
								}
							}
						}else if(item.isPos){
							for(let pIndex in this.newCheckList){
								if(this.newCheckList[pIndex].id == item.id && this.newCheckList[pIndex].isPos){
									this.newCheckList.splice(pIndex,1)
									this.newCheckListStr.splice(pIndex,1)
								}
							}
						}else if(item.isInst){
							for(let iIndex in this.newCheckList){
								if(this.newCheckList[iIndex].id == item.id && this.newCheckList[iIndex].isInst){
									this.newCheckList .splice(iIndex,1)
									this.newCheckListStr.splice(iIndex,1)
								}
							}
						}
					}
				}
				this.$forceUpdate()
			},
			toChildren(item){
				let _that = this
				if(_that.searchType == "byOri"){
					_that.getDataList(item).then(res=>{
						for(let resItem of res.value){
							resItem['checked'] = false
							resItem['isInst'] = true
							for(let checkItem of _that.newCheckList){
								if(checkItem.id == resItem.id){
									if(checkItem.isInst){
										_that.$set(resItem,'checked',true)
									}
								}
							}
						}
						_that.items = res.value
						if(!_that.searchUser){
							_that.stackList.push(item);
						}else{
							_that.$refs.search.cancel()
						}
						_that.$nextTick(() => {
							_that.scrollLeft += 200;
						})
						_that.$forceUpdate()
					})
				}else{
					if(_that.searchType == "bySearchBar"){
						if(_that.preSearchType == "byOri"){
							_that.getDataList(item).then(res=>{
								for(let resItem of res.value){
									resItem['checked'] = false
									resItem['isInst'] = true
									for(let checkItem of _that.newCheckList){
										if(checkItem.id == resItem.id){
											if(checkItem.isInst){
												_that.$set(resItem,'checked',true)
											}
										}
									}
								}
								_that.items = res.value
								if(!_that.searchUser){
									_that.stackList.push(item);
								}else{
									_that.$refs.search.cancel()
								}
								_that.$nextTick(() => {
									_that.scrollLeft += 200;
								})
								_that.$forceUpdate()
							})
						}
					}else{
						_that.getDataList1(item.id).then(res=>{
							for(let resItem of res){
								resItem['checked'] = false
								resItem['isPos'] = true
								for(let checkItem of _that.newCheckList){
									if(checkItem.id == resItem.id){
										if(checkItem.isPos){
											_that.$set(resItem,'checked',true)
										}
									}
								}
							}
							_that.items = res
							if(!_that.searchUser){
								_that.stackList.push(item);
							}else{
								_that.$refs.search.cancel()
							}
							_that.$nextTick(() => {
								_that.scrollLeft += 200;
							})
							_that.$forceUpdate()
						})
					}
				}
			},
			backTree(item,index){
				let _that = this,
					max = 66666,
					newStckList = JSON.parse(JSON.stringify( _that.stackList)) ;
					if (index == _that.stackList.length -1 && !_that.searchUser){
						return
					}else{
						if(_that.searchUser){
							_that.searchUser = !_that.searchUser
						}
					}
					if(item.isPos){
						_that.searchType ="byPos"
					}else if(item.isInst){
						_that.searchType ="byOri"
					}
					let stackItem = item
					if (index == -1) {
						_that.stackList.splice(1, max)
						_that.isre = false
						stackItem = {id:''}
					}else{
						if(_that.stackList.length-index >2){
							_that.stackList.forEach((stack,i)=>{
								if (i > index) {
									_that.stackList.splice(i, max)
								}
							})
						}else{
							if(_that.stackList.length != 1){
								_that.stackList.splice(_that.stackList.length - 1, 1)
							}
						}
					}
				if(_that.searchType =="byOri"){
					_that.getDataList(stackItem).then(res=>{
						for(let item of res.value){
							item['isInst'] = true
							for(let checkItem of _that.newCheckList){
								if(checkItem.id == item.id && checkItem.isInst){
									_that.$set(item,'checked',true)
								}
							}
						}
						_that.items = res.value
					})
				} else if(_that.searchType =="byPos" && item.id != -22 ){
					if(index == -1){
						_that.showMenuList = _that.menuList[1].children
						_that.showSearchType = true
					}else{
						_that.getDataList1(item.id).then(res=>{
							for(let resItem of res){
								resItem['checked'] = false
								resItem['isPos'] = true
								for(let checkItem of _that.newCheckList){
									if(checkItem.id == resItem.id){
										if(checkItem.isPos){
											_that.$set(resItem,'checked',true)
										}
									}
								}
							}
							_that.items = res
						})
					}
				}else{
					if(item){
						if(item.id == -21){
							_that.getCombobox()
							_that.showSearchType = true
						}else if(item.id == -22){
							_that.getDataList1().then(res=>{
								for(let resItem of res){
									resItem['checked'] = false
									resItem['isPos'] = true
									for(let checkItem of _that.newCheckList){
										if(checkItem.id == resItem.id){
											if(checkItem.isPos){
												_that.$set(resItem,'checked',true)
											}
										}
									}
								}
								_that.items = res
							})
							_that.showSearchType = false
						}
						if(index == -1){
							if(_that.searchType == "bySearchBar"){
								if(this.preSearchType == 'byLevel' || this.preSearchType == 'byPos'){
									_that.showMenuList = _that.menuList[1].children
									_that.showSearchType = true
								}else if(this.preSearchType == 'byOri'){
									_that.getDataList(stackItem).then(res=>{
										for(let item of res.value){
											for(let checkItem of _that.newCheckList){
												if(checkItem.id == item.id && checkItem.isInst){
													_that.$set(item,'checked',true)
												}
											}
										}
										_that.items = res.value
									})
								}
							}else if(_that.searchType == "byLevel" || _that.searchType == "byPos"){
								_that.showMenuList = _that.menuList[1].children
								_that.showSearchType = true
							}
						}
					}else{
						_that.showMenuList = _that.menuList
					}
				}
			}
		},
		mounted(){
			uni.getSystemInfo({
				success:(res)=>{
					this.height = res.windowHeight
				}
			})
			if(this.showTree){
				this.om.request(
					'temployee/findUserById'
				).then(res=>{
					this.om.request(
						"orgInstPosUserOfficial/findInstByUserId",
						{
							userid:res.value.id,
						}
					).then(res1=>{
						for(let item of res1){
							let obj = {
								id:item.id,
								label:item.instName
							}
							this.menuList.push(obj)
							this.showMenuList = this.menuList
						}
					})
				})
			}
		},
		onReachBottom(){
			let _that = this
			if(_that.paging && !_that.stopGetData){
				_that.page += 1
				_that.other.page = _that.page
				_that.getDataList2()
			}
		},
		onLoad(options){
			//抄送人
			this.isFormUser = options.isFormUser
			//缓存名
			this.cachename = options.cachename
			//多选时表单中已有人数
			this.userNum = options.userNum
			//选择类别 单选 多选(默认)
			if(options.isSingle && options.isSingle == 'true'){
				this.chooseType = 'single'
				this.maxNum = 1
			}
			//多选时已有人数
			if(options.hasNum){
			  this.maxNum -=  Number(options.hasNum)
			}
			//提示信息
			if(options.tips){
				this.tips = options.tips
			}
			// 请求参数
			if(options.other){
				this.other =  JSON.parse(options.other) 
			}
			//分页
			if(options.paging){
				this.paging = true
				Object.assign(this.other,{page:this.page,rows:this.row})
			}
			//查询参数
			if(options.searchfield){
				this.searchfield = options.searchfield
				this.$set(this.other,options.searchfield,'')
			}
			//请求路径
			if(options.url){
				this.url = options.url
				this.showSearchType = false 
				this.itemKey = 'id'
				this.showTree = false
				this.showPeopleNum = false
				this.getDataList2()
			}else{
				this.acquiescence = true
			}
			if(options.label){
				this.label = options.label
			}
			if(options.label2){
				this.label2 = options.label2
			}
			this.label3 = 'id'
			if(options.posIds){
				this.posIds = options.posIds
				this.showTree = false
				this.findPoslistByRequest(options.posIds)
			}
			if(options.namefield){
				this.namefield = options.namefield
			}
			if(options.showPosInfos == 'true'){
				this.showPosInfos = true
			}
			//人员初始选项
			if(options.hasDefaultData){
				uni.getStorage({
					key:'searchUserDefaultList',
					success:(res)=>{
						for(let item of res.data){
							this.$set(item,'isUser',true)
							this.$set(item,'id',item[options.changefield])
							if(!item['username']){
								this.$set(item,'username',item[this.label2])
							}
						}
						this.label2 = 'username'
						this.defaultData = res.data
						this.items = res.data
					}
				})
			}
			//岗位
			if(options.isUserPos){
				this.isUserPos = true
				if(options.userPosUrl){
					this.showSearchType = false
					this.showInline = false
					this.userPosUrl = options.userPosUrl
					this.userPosParam = options.param
					this.getUserlist(options.userPosUrl,options.param)
				}
				uni.getStorage({
					key:'searchUserDefaultList',
					success:(res)=>{
						if(res.data){
							for(let d of res.data){
								this.$set(d,'isUser',true)
								this.$set(d,'checked',true)
								this.newCheckList.push(d)
							}
						}
						uni.removeStorage({
							key:'searchUserDefaultList'
						})
					}
				})
			}
			if(options.isNew){
				this.isNew = true
				if(!options.defaultDataCache){
					return
				}
				uni.getStorage({
					key:options.defaultDataCache,
					success:(res)=>{
						if(res.data){
							for(let d of res.data){
								this.$set(d,'isUser',true)
								this.$set(d,'checked',true)
								this.newCheckList.push(d)
							}
						}
						uni.removeStorage({
							key:'searchUserDefaultList'
						})
					}
				})
			}
			if(options.searchPos){
				this.searchPos = true
			}else{
				this.menuList[1].children.push(
					{
						label:'岗位',
						id:-22
					}
				)
			}
			if(options.showPeopleNum){
				this.showPeopleNum = false
			}
			if(options.ownPos){
				this.menuList = []
			}
			// 搜索url
			if(options.searchUrl){
				this.url = options.searchUrl
			}
		}
	}
</script>

<style scoped>
	.stack{
		padding-left:30rpx ;
	}
	.head{
		 width: 100%;
		 background-color: #FFFFFF;
		 position: fixed;
		 z-index: 9999;
		 border-bottom: 5px solid #F1F1F1;
	}
	.inline-item{
		display: inline-block;
	}
	.active {
		color: #4297ED !important;
	}
	.none {
		color: #666666;
	}
	.institution-dataList{
		height:100rpx;
		display: flex; 
		flex-direction:row; 
		align-items: center;
	}
	.institution-name{
		font-size:32rpx; margin: 10px;
	}
	.institution-button{
		width:200rpx;
		font-size:32rpx;
		position: absolute;
		right: 15px;
		display: flex;
		flex-direction:row;
	} 
	.institution-button view{
		width: 100rpx;
		text-align: center;
	}
	.institution-button-line{
		border-left: 1px solid #5697F4;
	}
	.selectInst{
		color: #acacac;
	}
	.foot{
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		z-index: 9999;
		width: 100%;
		height: 100rpx;
		box-shadow: 0px -4px 5px #c8c8c8;
	}
	.foot-left{
		display: flex; flex-direction: column;  position: absolute;left: 0; width:70%;height: 100%;
	}
	.foot-left text{
		margin-left: 20rpx;
	}
	.foot-left-top{
		font-size: 32rpx;
		font-weight: 600;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	} 
	.foot-left-bottom{
		font-size: 25rpx;color:#666666;
		text-align: left;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		direction: rtl;
		unicode-bidi: bidi-override;
	}
	.foot-right{
		display:flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		right: 0; 
		width:30%;
		height: 100%;
	}
	.foot-right-button{
		border-radius: 13rpx;
		 color: #FFFFFF;
		 display: flex;
		 align-items: center;
		 justify-content: center;
		 flex-direction:column;
		 width: 90%;
		 height: 70%;
	}
	#popup{
		z-index: 9999;
	}
</style>  